<?php

/**
 * @copyright       : FOSP
 * @version         : 20120322
 * @name            : Season
 * @todo            : Season model
 * @author          : QuangTM
 */
class Season
{
    
    const SEASON_DETAIL = 'season_detail';
    
    const SEASON_BY_LEAGUE = 'season_by_league_list';
    
    /**
     *
     * @var Season
     */
    protected static $instance;

    protected function __construct()
    {
        
    }
    
    /**
     *
     * @return Season
     */
    public static function getInstance()
    {
        if (!isset(self::$instance) || !self::$instance instanceof self)
            self::$instance = new self;
        return self::$instance;
    }
    
    /**
     * Get list season by league
     * @param int $leagueID
     * @return array
     */
    public function getListSeasonByLeagueID($leagueID)
    {
        $memcacheInstance = Fpt_Global::getMemcacheInstance($leagueID);
        $keyCache = Fpt_Global::makeCacheKey(self::SEASON_BY_LEAGUE, array($leagueID));

        // Read cache
        $listSeasons = $memcacheInstance->read($keyCache);
        
        // Miss cache
        if ($listSeasons === FALSE)
        {
            try
            {
                $config = Fpt_Global::getApplicationIni();
                // Get db teamseason instance
                $seasonObj = Fpt_Business_Season::factory($config['database']['default']);

                // Get data from db
                $listSeasons = $seasonObj->getListSeasonByLeagueID($leagueID);
                
                // Write to cache
                Fpt_Global::writeMemcache($leagueID, $keyCache, $listSeasons);
            }
            catch (Exception $ex)
            {
                Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
                return FALSE;
            }
        }
        
        return $listSeasons;
    }
    
    public function getListSeasonByIDs($arrSeasonIDs)
    {
        $result = array();
        $arrMissCache = array();
        foreach ($arrSeasonIDs as $seasonID)
        {
            $memcacheInstance = Fpt_Global::getMemcacheInstance($seasonID);
            $keyCache = Fpt_Global::makeCacheKey(self::SEASON_DETAIL, $seasonID);
            $seasonDetail = $memcacheInstance->read($keyCache);
            if ($seasonDetail === FALSE)
            {
                array_push($arrMissCache, $seasonID);
            }
            else
            {
                array_push($result, $seasonDetail);
            }
        }
        
        // If miss some items
        if (count($arrMissCache))
        {
            // Get config 
            $config = Fpt_Global::getApplicationIni();
            
            // Get DB Obj
            $seasonMySQL = Fpt_Business_Season::factory($config['database']['default']);
            
            // Get multi article
            $arrMissSeasons = $seasonMySQL->getListSeasonByIDs($arrMissCache);
            
            // Write memcache for each
            foreach ($arrMissSeasons as $seasonDetail)
            {
                $keyCache = Fpt_Global::makeCacheKey(self::SEASON_DETAIL, $seasonDetail['SeasonID']);
                Fpt_Global::writeMemcache($seasonDetail['SeasonID'], $keyCache, $seasonDetail);
            }
            
            $result = array_merge($result, $arrMissSeasons);
        }
        return $result;
    }

}